package esqeee.xieqing.com.huangrs.doAction.api;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.accessibility.AccessibilityNodeInfo;

import com.xieqing.codeutils.util.ImageUtils;
import com.xieqing.codeutils.util.LogUtils;
import com.xieqing.codeutils.util.StringUtils;
import com.zhihu.matisse.internal.utils.BitmapUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import esqeee.xieqing.com.huangrs.action.ActionHelper;
import esqeee.xieqing.com.huangrs.bean.JSONBean;
import esqeee.xieqing.com.huangrs.doAction.Base;
import esqeee.xieqing.com.huangrs.library.MathResult;
import esqeee.xieqing.com.huangrs.library.RuntimeLog;
import esqeee.xieqing.com.huangrs.service.AccessbilityUtils;
import esqeee.xieqing.com.huangrs.user.User;
import esqeee.xieqing.com.huangrs.utils.HttpClient;

public class Node extends Base {
    public static Base intansce;

    public static Base getIntansce() {
        if (intansce == null) {
            intansce = new Node();
        }
        return intansce;
    }

    @Override
    public boolean post(JSONBean param) {
        waitForAccessbility();
        String arg = param.getString("arg");
        JSONBean var;
        int type;
        int exception;
        int comparison;
        int second;
        boolean isExist = false;
        String text;
        String content;
        MathResult index;
        List<AccessibilityNodeInfo> accessibilityNodeInfoList;
        switch (arg) {
            case "取当前窗口类名":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                setValue(var, AccessbilityUtils.getService().getCurrentWindowClassName());
                break;
            case "查询所有控件":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                type = param.getInt("type", 0);
                text = getString(param.getString("param"));
                comparison = param.getInt("comparison", 0);
                content = param.getString("text").trim();
                second = param.getInt("second", 10);
                accessibilityNodeInfoList = new ArrayList<>();
                for (int i = 0; i < second; i++) {
                    if (type == 0 || type == 1) {
                        AccessibilityNodeInfo root = AccessbilityUtils.getService().getRootInActiveWindow();
                        if (root != null) {
                            List<AccessibilityNodeInfo> all;
                            if (type == 0) {
                                all = root.findAccessibilityNodeInfosByViewId(text);
                            } else {
                                all = root.findAccessibilityNodeInfosByText(text);
                            }
                            if (StringUtils.isEmpty(content)) {
                                accessibilityNodeInfoList = all;
                                break;
                            } else {
                                this.getIncludeNodes(all, comparison, content, accessibilityNodeInfoList);
                                break;
                            }
                        }
                    } else if (type == 2) {
                        List<AccessibilityNodeInfo> nodesByClass = AccessbilityUtils.findNodesByClass(text);
                        RuntimeLog.e("<取控件>:共有控件数量:" + nodesByClass.size());
                        if (StringUtils.isEmpty(content)) {
                            accessibilityNodeInfoList = nodesByClass;
                            break;
                        } else {
                            this.getIncludeNodes(nodesByClass, comparison, content, accessibilityNodeInfoList);
                            break;
                        }
                    } else if (type == 3) {
                        List<AccessibilityNodeInfo> nodesByDescription = AccessbilityUtils.findNodesByDescription(text);
                        if (StringUtils.isEmpty(content)) {
                            accessibilityNodeInfoList = nodesByDescription;
                            break;
                        } else {
                            this.getIncludeNodes(nodesByDescription, comparison, content, accessibilityNodeInfoList);
                            break;
                        }
                    }
                    if (accessibilityNodeInfoList.size() == 0) {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                setValue(var, accessibilityNodeInfoList);
                break;
            case "查找控件并点击":
                if (!StringUtils.isEmpty(param.getString("isClick"))) {
                    var = queryVariable(param.getString("isClick"));
                    assignVar(arg, var, param.getString("isClick"));
                } else {
                    var = null;
                }
                type = param.getInt("type", 0);
                exception = param.getInt("exception", 0);
                comparison = param.getInt("comparison", 0);
                index = evalMath(param.getString("index", "0"));
                // 内容比较
                content = param.getString("text").trim();
                boolean isClick = false;
                if (index.getException() != null) {
                    RuntimeLog.e("<取控件>:计算索引错误:" + index.getException());
                    if (!StringUtils.isEmpty(param.getString("isClick"))) {
                        setValue(var, false);
                    }
                    break;
                }
                second = param.getInt("second", 10);
                text = getString(param.getString("param"));
                RuntimeLog.e("<取控件>:" + text + "," + content);
                for (int i = 0; i < second; i++) {
                    AccessibilityNodeInfo root = AccessbilityUtils.getService().getRootInActiveWindow();
                    if (type == 0) {
                        accessibilityNodeInfoList = root.findAccessibilityNodeInfosByViewId(text);
                    } else if (type == 1) {
                        accessibilityNodeInfoList = root.findAccessibilityNodeInfosByText(text);
                    } else if (type == 2) {
                        accessibilityNodeInfoList = AccessbilityUtils.findNodesByClass(text);
                    } else if (type == 3) {
                        accessibilityNodeInfoList = AccessbilityUtils.findNodesByDescription(text);
                    } else {
                        RuntimeLog.e("<查询所有控件>:没有定义查询方式：" + type);
                        break;
                    }
                    if (accessibilityNodeInfoList.size() > 0) {
                        if (index.getResult() >= accessibilityNodeInfoList.size()) {
                            RuntimeLog.e("<取控件>:超出数组下标，索引:" + index.getResult() + ",总大小：" + accessibilityNodeInfoList.size());
                            break;
                        }
                        // 如果没有填写点击控件的文本内容，则直接根据索引点击控件
                        if (StringUtils.isEmpty(content)) {
                            AccessibilityNodeInfo nodeInfo = accessibilityNodeInfoList.get(index.getResult());
                            if (nodeInfo == null) {
                                RuntimeLog.e("<" + arg + ">:控件为空");
                                break;
                            }
                            isClick = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            break;
                        } else {
                            // 有填写了控件文本内容，则需要判断控件的文本内容是否相等（忽略索引）
                            for (AccessibilityNodeInfo nodeInfo : accessibilityNodeInfoList) {
                                if (!StringUtils.isEmpty(nodeInfo.getText())) {
                                    if (comparison == 0 && nodeInfo.getText().toString().equalsIgnoreCase(content)) {
                                        isClick = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        break;
                                    } else if (comparison == 1 && nodeInfo.getText().toString().startsWith(content)) {
                                        isClick = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        break;
                                    } else if (comparison == 2 && nodeInfo.getText().toString().contains(content)) {
                                        isClick = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        break;
                                    } else if (comparison == 3 && nodeInfo.getText().toString().endsWith(content)) {
                                        isClick = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        break;
                                    }
                                }
                            }
                            if (!isClick) {
                                try {
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                RuntimeLog.e("<查找控件并点击>:控件存在并已点击:" + text + "," + content);
                                break;
                            }
                        }
                    } else {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (!isClick) {
                    RuntimeLog.e("<查找控件是否存在>:控件不存在:" + text + "," + content);
                }
                if (!StringUtils.isEmpty(param.getString("isClick"))) {
                    setValue(var, isClick);
                }
                RuntimeLog.e("检查是否需要推送：" + isClick + ", " + exception);
                if (!isClick && 0 != exception) {
                    LogUtils.i("需要推送", exception);
                    String appName = param.getString("appName").trim();
                    String message = param.getString("message").trim();
                    File file = null;
                    Bitmap bitmap = null;
                    try {
                        bitmap = getActionRun().getCaptruer().captrueScreen();
                        String filePath = ActionHelper.workSpaceDir + "captrueScreen.png";
                        file = BitmapUtil.saveBitmap2Local(bitmap, filePath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (bitmap != null) {
                            ImageUtils.recycle(bitmap);
                        }
                    }
                    // 推送
                    long currentTimeMillis = java.lang.System.currentTimeMillis();
                    String uri = "appRunError?appId=" + User.getUser().getMobile() + "&timestamp=" + currentTimeMillis + "&sign=" + MD5Utils.getSign(User.getUser().getMobile() + currentTimeMillis);
                    Map<String, String> params = new HashMap<>();
                    params.put("appName", appName);
                    params.put("message", message);
                    RuntimeLog.e("正在推送:" + params);
                    HttpClient.doPostWithFile(uri, null, params, file);
                    RuntimeLog.e("推送成功");
                }
                break;
            case "查找控件是否存在":
                if (!StringUtils.isEmpty(param.getString("isExist"))) {
                    var = queryVariable(param.getString("isExist"));
                    assignVar(arg, var, param.getString("isExist"));
                } else {
                    var = null;
                }
                type = param.getInt("type", 0);
                comparison = param.getInt("comparison", 0);
                index = evalMath(param.getString("index", "0"));
                isExist = false;
                if (index.getException() != null) {
                    RuntimeLog.e("<取控件>:计算索引错误:" + index.getException());
                    if (!StringUtils.isEmpty(param.getString("isClick"))) {
                        setValue(var, false);
                    }
                    break;
                }
                second = param.getInt("second", 10);
                text = getString(param.getString("param"));
                // 内容比较
                content = param.getString("text").trim();
                RuntimeLog.e("<取控件>:" + text + "," + content);
                for (int i = 0; i < second; i++) {
                    AccessibilityNodeInfo root = AccessbilityUtils.getService().getRootInActiveWindow();
                    if (type == 0) {
                        accessibilityNodeInfoList = root.findAccessibilityNodeInfosByViewId(text);
                    } else if (type == 1) {
                        accessibilityNodeInfoList = root.findAccessibilityNodeInfosByText(text);
                    } else if (type == 2) {
                        accessibilityNodeInfoList = AccessbilityUtils.findNodesByClass(text);
                    } else if (type == 3) {
                        accessibilityNodeInfoList = AccessbilityUtils.findNodesByDescription(text);
                    } else {
                        RuntimeLog.e("<查询所有控件>:没有定义查询方式：" + type);
                        break;
                    }
                    if (accessibilityNodeInfoList.size() > 0) {
                        if (index.getResult() >= accessibilityNodeInfoList.size()) {
                            RuntimeLog.e("<取控件>:超出数组下标，索引:" + index.getResult() + ",总大小：" + accessibilityNodeInfoList.size());
                            break;
                        }
                        // 如果没有填写点击控件的文本内容，则直接根据索引点击控件
                        if (StringUtils.isEmpty(content)) {
                            AccessibilityNodeInfo nodeInfo = accessibilityNodeInfoList.get(index.getResult());
                            isExist = !(nodeInfo == null);
                            break;
                        } else {
                            // 有填写了控件文本内容，则需要判断控件的文本内容是否相等（忽略索引）
                            for (AccessibilityNodeInfo nodeInfo : accessibilityNodeInfoList) {
                                if (!StringUtils.isEmpty(nodeInfo.getText())) {
                                    if (comparison == 0 && nodeInfo.getText().toString().equalsIgnoreCase(content)) {
                                        isExist = true;
                                        break;
                                    } else if (comparison == 1 && nodeInfo.getText().toString().startsWith(content)) {
                                        isExist = true;
                                        break;
                                    } else if (comparison == 2 && nodeInfo.getText().toString().contains(content)) {
                                        isExist = true;
                                        break;
                                    } else if (comparison == 3 && nodeInfo.getText().toString().endsWith(content)) {
                                        isExist = true;
                                        break;
                                    }
                                }
                            }
                            if (!isExist) {
                                try {
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                RuntimeLog.e("<查找控件是否存在>:控件存在" + text + "," + content);
                                break;
                            }
                        }
                    } else {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (!isExist) {
                    RuntimeLog.e("<查找控件是否存在>:控件不存在:" + text + "," + content);
                }
                if (!StringUtils.isEmpty(param.getString("isExist"))) {
                    setValue(var, isExist);
                }
                break;
            case "在控件下找控件点击":
                if (!StringUtils.isEmpty(param.getString("isClick"))) {
                    var = queryVariable(param.getString("isClick"));
                    assignVar(arg, var, param.getString("isClick"));
                } else {
                    var = null;
                }
                JSONBean current = queryVariable(param.getString("parent"));
                assignVar(arg, current, param.getString("parent"));
                AccessibilityNodeInfo nodeValue = getNodeValue(current);
                List<AccessibilityNodeInfo> childrenNodeList = new ArrayList<>();
                // 取控件下所有子控件
                this.getChildren(nodeValue, childrenNodeList);
                RuntimeLog.e("共找到多少个子控件：" + childrenNodeList.size());
                String childClass = getString(param.getString("child"));
                int childComparison = param.getInt("childComparison", 0);
                String childContent = param.getString("childText").trim();
                isClick = clickChild(childrenNodeList, childClass, childComparison, childContent);
                if (isClick) {
                    RuntimeLog.e("<在控件下找控件点击>:控件存在并点击：" + getString(param.getString("child")) + "," + param.getString("childText").trim());
                }
                if (!isClick) {
                    RuntimeLog.e("<在控件下找控件点击>:控件不存在：" + getString(param.getString("child")) + "," + param.getString("childText").trim());
                }
                setValue(var, isClick);
                break;
            case "查找控件中心位置":
                JSONBean varX;
                JSONBean varY;
                JSONBean nodeExist;
                if (!StringUtils.isEmpty(param.getString("positionX"))) {
                    varX = queryVariable(param.getString("positionX"));
                    assignVar(arg, varX, param.getString("positionX"));
                } else {
                    varX = null;
                }
                if (!StringUtils.isEmpty(param.getString("positionY"))) {
                    varY = queryVariable(param.getString("positionY"));
                    assignVar(arg, varY, param.getString("positionY"));
                } else {
                    varY = null;
                }
                if (!StringUtils.isEmpty(param.getString("isExist"))) {
                    nodeExist = queryVariable(param.getString("isExist"));
                    assignVar(arg, nodeExist, param.getString("isExist"));
                } else {
                    nodeExist = null;
                }
                type = param.getInt("type", 0);
                comparison = param.getInt("comparison", 0);
                index = evalMath(param.getString("index", "0"));
                if (index.getException() != null) {
                    RuntimeLog.e("<取控件>:计算索引错误:" + index.getException());
                    if (!StringUtils.isEmpty(param.getString("position"))) {
                        setValue(varX, "");
                        setValue(varY, "");
                        setValue(nodeExist, false);
                    }
                    break;
                }
                String positionX = null;
                String positionY = null;
                List<Integer> positionList;
                second = param.getInt("second", 10);
                text = getString(param.getString("param"));
                // 内容比较
                content = param.getString("text").trim();
                RuntimeLog.e("<取控件>:" + text + "," + content);
                for (int i = 0; i < second; i++) {
                    AccessibilityNodeInfo root = AccessbilityUtils.getService().getRootInActiveWindow();
                    if (type == 0) {
                        accessibilityNodeInfoList = root.findAccessibilityNodeInfosByViewId(text);
                    } else if (type == 1) {
                        accessibilityNodeInfoList = root.findAccessibilityNodeInfosByText(text);
                    } else if (type == 2) {
                        accessibilityNodeInfoList = AccessbilityUtils.findNodesByClass(text);
                    } else if (type == 3) {
                        accessibilityNodeInfoList = AccessbilityUtils.findNodesByDescription(text);
                    } else {
                        RuntimeLog.e("<查询所有控件>:没有定义查询方式：" + type);
                        break;
                    }
                    if (accessibilityNodeInfoList.size() > 0) {
                        if (index.getResult() >= accessibilityNodeInfoList.size()) {
                            RuntimeLog.e("<取控件>:超出数组下标，索引:" + index.getResult() + ",总大小：" + accessibilityNodeInfoList.size());
                            break;
                        }
                        // 如果没有填写点击控件的文本内容，则直接根据索引点击控件
                        if (StringUtils.isEmpty(content)) {
                            AccessibilityNodeInfo nodeInfo = accessibilityNodeInfoList.get(index.getResult());
                            positionList = getNodePosition(nodeInfo);
                            positionX = positionList.get(0).toString();
                            positionY = positionList.get(1).toString();
                            isExist = true;
                            break;
                        } else {
                            // 有填写了控件文本内容，则需要判断控件的文本内容是否相等（忽略索引）
                            for (AccessibilityNodeInfo nodeInfo : accessibilityNodeInfoList) {
                                if (!StringUtils.isEmpty(nodeInfo.getText())) {
                                    if (comparison == 0 && nodeInfo.getText().toString().equalsIgnoreCase(content)) {
                                        positionList = getNodePosition(nodeInfo);
                                        positionX = positionList.get(0).toString();
                                        positionY = positionList.get(1).toString();
                                        isExist = true;
                                        break;
                                    } else if (comparison == 1 && nodeInfo.getText().toString().startsWith(content)) {
                                        positionList = getNodePosition(nodeInfo);
                                        positionX = positionList.get(0).toString();
                                        positionY = positionList.get(1).toString();
                                        isExist = true;
                                        break;
                                    } else if (comparison == 2 && nodeInfo.getText().toString().contains(content)) {
                                        positionList = getNodePosition(nodeInfo);
                                        positionX = positionList.get(0).toString();
                                        positionY = positionList.get(1).toString();
                                        isExist = true;
                                        break;
                                    } else if (comparison == 3 && nodeInfo.getText().toString().endsWith(content)) {
                                        positionList = getNodePosition(nodeInfo);
                                        positionX = positionList.get(0).toString();
                                        positionY = positionList.get(1).toString();
                                        isExist = true;
                                        break;
                                    }
                                }
                            }
                            if (StringUtils.isEmpty(positionX) || StringUtils.isEmpty(positionY)) {
                                try {
                                    Thread.sleep(1000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                RuntimeLog.e("<查找控件是否存在>:控件存在" + text + "," + content);
                                break;
                            }
                        }
                    } else {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (!isExist) {
                    RuntimeLog.e("<查找控件是否存在>:控件不存在:" + text + "," + content);
                }
                if (!StringUtils.isEmpty(param.getString("positionX"))) {
                    setValue(varX, positionX);
                }
                if (!StringUtils.isEmpty(param.getString("positionY"))) {
                    setValue(varY, positionY);
                }
                if (!StringUtils.isEmpty(param.getString("isExist"))) {
                    setValue(nodeExist, isExist);
                }
                break;
            case "取查询控件总数":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                JSONBean var2 = queryVariable(param.getString("var2"));
                assignVar(arg, var, param.getString("var2"));

                List<AccessibilityNodeInfo> nodeInfos = getNodesValue(var2);
                setValue(var, nodeInfos == null ? 0 : nodeInfos.size());
                break;
            case "取子控件数":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                var2 = queryVariable(param.getString("var2"));
                assignVar(arg, var, param.getString("var2"));

                AccessibilityNodeInfo node = getNodeValue(var2);
                setValue(var, node == null ? 0 : node.getChildCount());
                break;
            case "取控件矩阵":
                var = queryVariable(param.getString("var"));
                node = getNodeValue(var);
                if (node != null) {
                    Rect rect = new Rect();
                    node.getBoundsInScreen(rect);
                    setValue(queryVariable(param.getString("left")), rect.left);
                    setValue(queryVariable(param.getString("top")), rect.top);
                    setValue(queryVariable(param.getString("width")), rect.width());
                    setValue(queryVariable(param.getString("height")), rect.height());
                }
                break;
            case "取控件内容":
            case "取控件类名":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                var2 = queryVariable(param.getString("var2"));
                assignVar(arg, var, param.getString("var2"));

                node = getNodeValue(var2);
                text = "";
                switch (arg) {
                    case "取控件内容":
                        text = node == null ? "" : node.getText() == null ? "" : node.getText().toString();
                        break;
                    case "取控件类名":
                        text = node == null ? "" : node.getClassName() == null ? "" : node.getClassName().toString();
                        break;
                }
                setValue(var, text);
                break;
            case "取控件":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                var2 = queryVariable(param.getString("var2"));
                assignVar(arg, var, param.getString("var2"));
                index = evalMath(param.getString("index", "0"));
                if (index.getException() != null) {
                    RuntimeLog.e("<取控件>:计算索引错误:" + index.getException());
                    break;
                }

                nodeInfos = getNodesValue(var2);
                if (nodeInfos == null) {
                    setValue(var, "空");
                } else {
                    if (index.getResult() >= nodeInfos.size()) {
                        RuntimeLog.e("<取控件>:超出数组下标，索引:" + index.getResult() + ",总大小：" + nodeInfos.size());
                        break;
                    } else {
                        setValue(var, nodeInfos.get(index.getResult()));
                    }
                }
                break;
            case "取子控件":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                var2 = queryVariable(param.getString("var2"));
                assignVar(arg, var, param.getString("var2"));
                index = evalMath(param.getString("index", "0"));
                if (index.getException() != null) {
                    RuntimeLog.e("<取子控件>:计算索引错误:" + index.getException());
                    break;
                }

                node = getNodeValue(var2);
                if (node == null) {
                    setValue(var, "空");
                } else {
                    if (index.getResult() >= node.getChildCount()) {
                        RuntimeLog.e("<取子控件>:超出数组下标，索引:" + index.getResult() + ",总大小：" + node.getChildCount());
                        break;
                    } else {
                        setValue(var, node.getChild(index.getResult()));
                    }
                }
                break;
            case "取父控件":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                var2 = queryVariable(param.getString("var2"));
                assignVar(arg, var, param.getString("var2"));
                node = getNodeValue(var2);
                if (node == null) {
                    setValue(var, "空");
                } else {
                    setValue(var, node.getParent());
                }
                break;
            case "设置控件内容":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                text = param.getString("text");
                text = getString(text);

                AccessbilityUtils.setNodeText(getNodeValue(var), text);
                break;
            case "反向滑动控件":
            case "正向滑动控件":
            case "点击控件":
            case "长按控件":
            case "获取焦点":
                var = queryVariable(param.getString("var"));
                assignVar(arg, var, param.getString("var"));
                boolean result = false;
                AccessibilityNodeInfo nodeInfo = getNodeValue(var);
                if (nodeInfo == null) {
                    RuntimeLog.i("<" + arg + ">:控件为空");
                } else {
                    switch (arg) {
                        case "反向滑动控件":
                            result = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
                            break;
                        case "正向滑动控件":
                            result = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            break;
                        case "点击控件":
                            result = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            break;
                        case "长按控件":
                            result = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
                            break;
                        case "获取焦点":
                            result = nodeInfo.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
                            break;
                    }
                }
                log("<" + arg + ">:" + (result ? "成功" : "失败"));
                break;
        }
        return true;
    }

    private void getChildren(AccessibilityNodeInfo nodeInfo, List<AccessibilityNodeInfo> children) {
        if (nodeInfo != null && nodeInfo.getChildCount() > 0) {
            for (int i = 0; i < nodeInfo.getChildCount(); i++) {
                AccessibilityNodeInfo child = nodeInfo.getChild(i);
                if (child != null && child.getChildCount() > 0) {
                    getChildren(child, children);
                } else if(child != null){
                    children.add(child);
                }
            }
        }
    }

    private void getIncludeNodes(List<AccessibilityNodeInfo> accessibilityNodeInfoList, int childComparison, String childContent, List<AccessibilityNodeInfo> includeList) {
        for (AccessibilityNodeInfo accessibilityNodeInfo : accessibilityNodeInfoList) {
            if (accessibilityNodeInfo.getText() == null) {
                continue;
            }
            RuntimeLog.e("<取控件>:控件内容:" + accessibilityNodeInfo.getText().toString() + ", 比较内容：" + childContent);
            if (childComparison == 0 && accessibilityNodeInfo.getText().toString().equalsIgnoreCase(childContent)) {
                includeList.add(accessibilityNodeInfo);
            } else if (childComparison == 1 && accessibilityNodeInfo.getText().toString().startsWith(childContent)) {
                includeList.add(accessibilityNodeInfo);
            } else if (childComparison == 2 && accessibilityNodeInfo.getText().toString().contains(childContent)) {
                includeList.add(accessibilityNodeInfo);
            } else if (childComparison == 3 && accessibilityNodeInfo.getText().toString().endsWith(childContent)) {
                includeList.add(accessibilityNodeInfo);
            }
        }
        RuntimeLog.e("<取控件>:共有几个符合条件:" + includeList.size());
    }

    private boolean clickChild(List<AccessibilityNodeInfo> accessibilityNodeInfoList, String childClass, int childComparison, String childContent) {
        for (AccessibilityNodeInfo nodeInfo : accessibilityNodeInfoList) {
            if (StringUtils.isEmpty(childContent)) {
                return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            } else {
                if (nodeInfo.getClassName() == null) {
                    continue;
                }
                if(!childClass.equalsIgnoreCase(nodeInfo.getClassName().toString())){
                    continue;
                }
                if (nodeInfo.getText() == null) {
                    continue;
                }
                if (childComparison == 0 && nodeInfo.getText().toString().equalsIgnoreCase(childContent)) {
                    return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                } else if (childComparison == 1 && nodeInfo.getText().toString().startsWith(childContent)) {
                    return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                } else if (childComparison == 2 && nodeInfo.getText().toString().contains(childContent)) {
                    return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                } else if (childComparison == 3 && nodeInfo.getText().toString().endsWith(childContent)) {
                    return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
        return false;
    }

    public List<AccessibilityNodeInfo> getNodesValue(JSONBean var) {
        if (var == null) {
            return null;
        }
        Object nodeInfos = var.get("value");
        if (nodeInfos != null) {
            if (nodeInfos instanceof ArrayList) {
                return (List<AccessibilityNodeInfo>) nodeInfos;
            }
        }
        return null;
    }

    private List<Integer> getNodePosition(AccessibilityNodeInfo node) {
        Rect rect = new Rect();
        node.getBoundsInScreen(rect);
        int x = (rect.left + rect.right) / 2;
        int y = (rect.top + rect.bottom) / 2;
        List<Integer> integers = new ArrayList<>();
        integers.add(x);
        integers.add(y);
        return integers;
    }

    public AccessibilityNodeInfo getNodeValue(JSONBean var) {
        if (var == null) {
            return null;
        }
        Object nodeInfos = var.get("value");
        if (nodeInfos != null) {
            if (nodeInfos instanceof AccessibilityNodeInfo) {
                return (AccessibilityNodeInfo) nodeInfos;
            }
        }
        return null;
    }

    public void setValue(JSONBean var, List<AccessibilityNodeInfo> nodeInfos) {
        if (var == null) {
            return;
        }
        var.put("value", nodeInfos);
    }

    public void setValue(JSONBean var, AccessibilityNodeInfo nodeInfo) {
        if (var == null) {
            return;
        }
        var.put("value", nodeInfo);
    }

    public void recyle(JSONBean var) {
        Object nodeInfos = var.get("value");
        if (nodeInfos != null) {
            if (nodeInfos instanceof ArrayList) {
                AccessbilityUtils.recyleNodes((List<AccessibilityNodeInfo>) nodeInfos);
            }
            if (nodeInfos instanceof AccessibilityNodeInfo) {
                ((AccessibilityNodeInfo) nodeInfos).recycle();
            }
        }
    }

    private void assignVar(String arg, JSONBean var, String var1) {
        if (var == null) {
            RuntimeLog.e("<" + arg + ">:错误，找不到变量[" + var1 + "]");
            getActionRun().stopDo();
        }
    }

    private boolean writeByte(String file, byte[] bytes) {
        try {
            java.io.File file1 = new java.io.File(file);
            FileOutputStream fileOutputStream = new FileOutputStream(file1);
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int getType() {
        return 2;
    }

    @Override
    public String getName() {
        return "文件操作";
    }
}
